package com.fessc.develop.handler.processor.callback;

import com.alibaba.fastjson.JSON;
import com.fessc.develop.base.CCBResult;
import com.fessc.develop.entity.ccbvo.CCBSettleTradeCallBackVo;
import com.fessc.develop.entity.dbvo.TPayment;
import com.fessc.develop.entity.ccbvo.CCBPrePaymentCallBackVo;
import com.fessc.develop.entity.ccbvo.CCBResponseBodyVo;
import com.fessc.develop.entity.ccbvo.CCBResponseHeadVo;
import com.fessc.develop.model.enums.*;
import com.fessc.develop.service.interfaces.ITPaymentService;
import com.fessc.develop.utils.BusiUtil;
import com.fessc.develop.utils.ErrorUtil;
import com.fessc.develop.utils.ExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class CCBPrePaymentCallBackProcessor implements CCBICallBackProcessor {

    @Resource
    private ITPaymentService paymentService;

    @Override
    public String getServiceCode() {
        return EnumServiceCode.CCB_HMT005.toString();
    }

    /**
     * 预付款、退款交易结果回调
     *
     * @param ccbPrePaymentCallBackVo 预付款交易结果回调报文
     * @return 返回结果
     */
    @Override
    public CCBResult<CCBResponseBodyVo> paymentStatusProcess(CCBPrePaymentCallBackVo ccbPrePaymentCallBackVo) {
        log.info("建行预付款退款回调通知处理开始");
        CCBResponseHeadVo responseHeadVo = new CCBResponseHeadVo(EnumResultType.CCB_SUCCESS.getStatusCode(), EnumResultType.CCB_SUCCESS.getMsg());
        CCBResponseBodyVo responseBodyVo = new CCBResponseBodyVo(EnumResultType.CCB_SUCCESS.getStatusCode(), EnumResultType.CCB_SUCCESS.getMsg());
        String paymentFlag = "";

        //判断订单编号、业务类型、交易结果是否为空，是否符合报文要求
        String errorCode = check(ccbPrePaymentCallBackVo);
        if (BusiUtil.isNotNull(errorCode)) {
            responseBodyVo.setFalrs(ErrorUtil.getErrorDesc(errorCode));
            responseBodyVo.setPsgrlt(EnumResultType.CCB_FAIL.getStatusCode());
            return new CCBResult<>(responseHeadVo, responseBodyVo);
        }

        //判断订单编号是否正确
        TPayment tPayment = paymentService.getTPaymentByOrderNo(ccbPrePaymentCallBackVo.getOrdrno());
        String[] args = {ccbPrePaymentCallBackVo.getOrdrno()};
        if (BusiUtil.isNull(tPayment)) {
            responseBodyVo.setFalrs(ErrorUtil.getParseErrorDesc("EE1005", args));
            responseBodyVo.setPsgrlt(EnumResultType.CCB_FAIL.getStatusCode());
            return new CCBResult<>(responseHeadVo, responseBodyVo);
        }

        //根据请求报文更新交易状态
        if (BusiUtil.isEquals(ccbPrePaymentCallBackVo.getTxntp(), EnumCallBackOption.PRE_PAYMENT_CALL_BACK.toString())) {  //预付款
            if (BusiUtil.isEquals(ccbPrePaymentCallBackVo.getTxnrlt(), EnumResultType.CCB_SUCCESS.getStatusCode())) {
                log.info("建行预付款交易结果回调通知，交易成功：" + tPayment.getSuborderno());
                paymentFlag = EnumPaymentStatus.PAYMENT_SUCCESS.toString();
                tPayment.setPaymentStatus(EnumPaymentStatus.PAYMENT_SUCCESS.toString()); //银行扣款成功
                tPayment.setPaymentLog(EnumPaymentStatus.PAYMENT_SUCCESS_LOG.toString());
                tPayment.setBankRefids(ccbPrePaymentCallBackVo.getTxnscsstraceid());
                tPayment.setPaymentWorkdate(ccbPrePaymentCallBackVo.getReqTime());
            } else if (BusiUtil.isEquals(ccbPrePaymentCallBackVo.getTxnrlt(), EnumResultType.CCB_FAIL.getStatusCode())) {
                log.info("建行预付款交易结果回调通知，交易失败：" + tPayment.getSuborderno());
                paymentFlag = EnumPaymentStatus.PAYMENT_FAIL.toString();
                tPayment.setPaymentStatus(ccbPrePaymentCallBackVo.getTxnrlt()); //银行扣款失败败
                tPayment.setPaymentLog(BusiUtil.nvl(ccbPrePaymentCallBackVo.getTxnrltdsc(), EnumPaymentStatus.PAYMENT_FAIL_LOG.toString()));
            } else {
                log.info("建行预付款交易结果回调通知，返回状态字段值错误：" + ccbPrePaymentCallBackVo.getTxnrlt());
            }
        } else if (BusiUtil.isEquals(ccbPrePaymentCallBackVo.getTxntp(), EnumCallBackOption.PRE_REFUND_CALL_BACK.toString())) {  //预付款退款
            if (BusiUtil.isEquals(ccbPrePaymentCallBackVo.getTxnrlt(), EnumResultType.CCB_SUCCESS.getStatusCode())) {
                log.info("建行预付款退款交易结果回调通知，交易成功：" + tPayment.getSuborderno());
                paymentFlag = EnumPaymentStatus.PAYMENT_SUCCESS.toString();
                tPayment.setRefundStatus(EnumPaymentStatus.PAYMENT_SUCCESS.toString()); //银行扣款成功
                tPayment.setRefundLog(EnumPaymentStatus.PAYMENT_SUCCESS_LOG.toString());
                tPayment.setRefundRefids(ccbPrePaymentCallBackVo.getTxnscsstraceid());
                tPayment.setRefundWorkdate(ccbPrePaymentCallBackVo.getReqTime());
                tPayment.setSend2("N");
            } else if (BusiUtil.isEquals(ccbPrePaymentCallBackVo.getTxnrlt(), EnumResultType.CCB_FAIL.getStatusCode())) {
                log.info("建行预付款退款交易结果回调通知，交易失败：" + tPayment.getSuborderno());
                paymentFlag = EnumPaymentStatus.PAYMENT_FAIL.toString();
                tPayment.setRefundStatus(ccbPrePaymentCallBackVo.getTxnrlt());
                tPayment.setRefundLog(BusiUtil.nvl(ccbPrePaymentCallBackVo.getTxnrltdsc(), EnumPaymentStatus.PAYMENT_FAIL_LOG.toString()));
            } else {
                log.info("建行预付款退款交易结果回调通知，返回状态字段值错误：" + ccbPrePaymentCallBackVo.getTxnrlt());
            }
        }

        //更新预付款交易结果
        if (BusiUtil.isEquals(EnumCallBackOption.PRE_PAYMENT_CALL_BACK.toString(), ccbPrePaymentCallBackVo.getTxntp())) {
            // 更新预付款表交易结果
            if (!paymentService.updatePrePaymentStatusRefidsLog(tPayment, paymentFlag)) {
                throw ExceptionUtil.businessException("EE1004");
            }
        } else if (BusiUtil.isEquals(EnumCallBackOption.PRE_REFUND_CALL_BACK.toString(), ccbPrePaymentCallBackVo.getTxntp())) {
            if (!paymentService.updateRefundStatusRefidsLog(tPayment, paymentFlag)) {
                throw ExceptionUtil.businessException("EE1004");
            }
        }

        // 返回响应
        CCBResult<CCBResponseBodyVo> ccbResult = new CCBResult<>(responseHeadVo, responseBodyVo);
        log.info("建行预付款回调通知处理结束，返回报文：" + JSON.toJSONString(ccbResult));
        return ccbResult;
    }

    private String check(CCBPrePaymentCallBackVo ccbPrePaymentCallBackVo) {

        if (BusiUtil.isNull(ccbPrePaymentCallBackVo) || BusiUtil.isNull(ccbPrePaymentCallBackVo.getOrdrno())) {
            return "EE1010";
        } else if (BusiUtil.isNull(ccbPrePaymentCallBackVo.getOrdrno())) {
            return "EE1009";
        } else if (BusiUtil.isNotEquals(EnumCallBackOption.PRE_PAYMENT_CALL_BACK.toString(), ccbPrePaymentCallBackVo.getTxntp())
                && BusiUtil.isNotEquals(EnumCallBackOption.PRE_REFUND_CALL_BACK.toString(), ccbPrePaymentCallBackVo.getTxntp())) {
            return "EE1007";
        } else if (BusiUtil.isNotEquals(EnumResultType.CCB_SUCCESS.getStatusCode(), ccbPrePaymentCallBackVo.getTxnrlt())
                && BusiUtil.isNotEquals(EnumResultType.CCB_FAIL.getStatusCode(), ccbPrePaymentCallBackVo.getTxnrlt())) {
            return "EE1008";
        }
        return "";
    }

    /**
     * 跨境付汇交易结果回调
     *
     * @param ccbSettleTradeCallBackVo 跨境付汇交易结果回调报文
     * @return 返回结果
     */
    @Override
    public CCBResult settleTradeStatusProcess(CCBSettleTradeCallBackVo ccbSettleTradeCallBackVo) {
        return null;
    }

}
